#include "Mazeprinter.h"

Mazeprinter::Mazeprinter(Maze* input, bool printPoisson)
{
    //initializes variables
    maze = input;
    //if printPoisson is true, then print the Poisson Disks
    if (printPoisson){
        maze->printPoisson();
    }
    p = maze->getPoints();
    e = maze->Maze::getEdges();
    t = maze->getTriangles();
}

Mazeprinter::~Mazeprinter()
{
}

void Mazeprinter::printpointers()
{
    cout<<"#Points"<<endl;
	unsigned int x = 0;
	//prints out all the points in a sorted list by X then Y
    while (x<p.size())
    {
        p[x]->print();
        x++;
    }

    cout<<endl;
}

void Mazeprinter::printedges()
{
    cout<<"#Lines"<<endl;
    //prints out all the edges labeled by the index of points
    unsigned int j = 0;

    //this loop loops through all the edges
    while (j<e.size())
    {
        unsigned int k = 0;
        //this loop loops through all the pointers if a pointer is found, then the index of that pointer is printed instead of point itself
        while (k<p.size())
        {
            if (p[k]->equals(e[j]->getPoint1()))
            {
                cout<<k<<" ";

            }

            if (p[k]->equals(e[j]->getPoint2()))
            {
                cout<<k<<" ";
            }
        k++;
        }
        cout <<e[j]->status<<endl;
    j++;
    }
    cout<<endl;
}


void Mazeprinter::printrooms()
{
    cout<<"#Rooms"<<endl;
    unsigned int z = 0;

    //loop through all the triangles
    while (z<t.size())
    {
        cout<<"3 ";
        unsigned int q = 0;

        //create edges using the three points in the triangle to compare with the edges in the edgelist
        Edge* edge1 = new Edge(t[z]->getPoint1(), t[z]->getPoint2());
        Edge* edge2 = new Edge(t[z]->getPoint1(), t[z]->getPoint3());
        Edge* edge3 = new Edge(t[z]->getPoint2(), t[z]->getPoint3());

        //loop through all the edges to see if any of the newly created edges equal any of the edges in the edgelist then prints the index of the edges
        while (q<e.size())
        {
            if (e[q]->equals(edge1))
            {
                cout<<q<<" ";
            }
            if (e[q]->equals(edge2))
            {
                cout<<q<<" ";
            }
            if (e[q]->equals(edge3))
            {
                cout<<q<<" ";
            }
            q++;
        }
        cout<<endl;
        z++;
    }

    unsigned int l = 0;
    int entr, exit;
    //loop that checks for the entrance for printing
    while(l<t.size())
    {
        if (t[l]->isEntrance())
        entr = l;
        l++;
    }
    l = 0;
    //loop that checks if triangle is an exit for printing
    while (l<t.size())
    {
        if (t[l]->isExit())
        exit = l;
        l++;
    }
    cout<< entr << " " << exit << endl;
}

void Mazeprinter::print()
{
    //prints all files
    cout<<maze->width<< " "<<maze->height<<endl;
    cout<<p.size()<<endl;
    cout<<e.size()<<endl;
    cout<<t.size()<<endl;
    cout<<endl;
    printpointers();
    printedges();
    printrooms();
}
